package mk.company.fin.frame.internal;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.beans.PropertyVetoException;
import java.util.List;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.table.DefaultTableModel;

import mk.company.fin.AppConstants;
import mk.company.fin.model.Konto;
import mk.company.fin.model.KontoClass;
import mk.company.fin.model.KontoGroup;
import mk.company.fin.model.KontoSynthetic;
import mk.company.fin.service.KontoService;
import mk.company.fin.service.MessageService;
import mk.company.fin.service.ModelService;

import org.springframework.context.ApplicationContext;

public class ContoPlanFrame extends AbstractInternalFrame {

	private static final long serialVersionUID = 118706881759699303L;

	/* SERVICES */
	
	private MessageService messageService;
	private KontoService kontoService;
	private ModelService modelService;
	
	//component which this frame will be drawn
	JComponent component;
	public ContoPlanFrame(JComponent component) {
		this.component=component;
		
	}
	
	@Override
	public void initialize(ApplicationContext context) {
		setContext(context);
		injectServices();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(new Dimension(AppConstants.MainFrame.WIDTH, AppConstants.MainFrame.HEIGHT));
		// components
		initComponents();
	}

	/**
	 * Inject services.
	 */
	private void injectServices() {
		messageService = getContext().getBean(MessageService.class);
		kontoService=getContext().getBean(KontoService.class);
		modelService=getContext().getBean(ModelService.class);
	}
	
	@Override
	public void draw() {
		injectServices();
		initComponents();
	}

	public void initComponents() {
		// title
		setTitle(messageService.getMessage("comintent.conto.plan"));
		// Size
		this.setSize(AppConstants.MainFrame.WIDTH, AppConstants.MainFrame.HEIGHT);
		// Maximize
		try {
			this.setMaximum(true);
		} catch (PropertyVetoException e) {
		} catch (NullPointerException n) {
		}
		// Layout
		this.setLayout(null);
		
		//Initialize the ContoPlanDataPanel
		initContoPlanDataPanel();
		
		//Initialize the ContoPlanTablePanel
		initContoPlanTablePanel();
		
		initActionPanel();
		
		//Set the layout of all the panels
		setLayout();
		
		contoPlanDataPanel.setVisible(false);
		
		this.setVisible(true);
		component.add(this);
	}
	
	
	private javax.swing.JTextField analyticDescriptionField;
    private javax.swing.JTextField analyticNumberField;
    private javax.swing.JLabel analyticNumberLabel;
    private javax.swing.JTextField classDescriptionField;
    private javax.swing.JTextField classNumberField;
    private javax.swing.JLabel classNumberLabel;
    private javax.swing.JTextField contoField;
    private javax.swing.JLabel contoLabel;
    private javax.swing.JPanel contoPlanDataPanel;
    private javax.swing.JTextField groupDescriptionField;
    private javax.swing.JTextField groupNumberField;
    private javax.swing.JLabel groupNumberLabel;
    private javax.swing.JTextField syntheticDescriptionField;
    private javax.swing.JTextField syntheticNumberField;
    private javax.swing.JLabel syntheticNumberLabel;
    private javax.swing.JButton kontoSaveButton;
    
	private void initContoPlanDataPanel(){
		contoPlanDataPanel = new javax.swing.JPanel();
		contoPlanDataPanel.setBorder(BorderFactory.createTitledBorder(messageService.getMessage("conto")));
		
        classNumberLabel = new javax.swing.JLabel(messageService.getMessage("conto.class"));
        classNumberField = new javax.swing.JTextField();
        classNumberField.setColumns(1);
        groupNumberLabel = new javax.swing.JLabel(messageService.getMessage("conto.group"));
        groupNumberField = new javax.swing.JTextField();
        groupNumberField.setColumns(1);
        syntheticNumberLabel = new javax.swing.JLabel(messageService.getMessage("conto.synthetic"));
        syntheticNumberField = new javax.swing.JTextField();
        syntheticNumberField.setColumns(1);
        analyticNumberLabel = new javax.swing.JLabel(messageService.getMessage("conto.analytic"));
        analyticNumberField = new javax.swing.JTextField();
        analyticNumberField.setColumns(4);
        contoLabel = new javax.swing.JLabel(messageService.getMessage("conto"));
        contoField = new javax.swing.JTextField();
        contoField.setColumns(10);
        contoField.setEditable(false);
        classDescriptionField = new javax.swing.JTextField();
        classDescriptionField.setColumns(65);
        groupDescriptionField = new javax.swing.JTextField();
        groupDescriptionField.setColumns(65);
        analyticDescriptionField = new javax.swing.JTextField();
        analyticDescriptionField.setColumns(65);
        syntheticDescriptionField = new javax.swing.JTextField();
        syntheticDescriptionField.setColumns(65);
        kontoSaveButton = new javax.swing.JButton();
        kontoSaveButton.setText(messageService.getMessage("conto.save"));
        
        kontoSaveButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				//Save conto
				String classText=classNumberField.getText();
				String groupText=groupNumberField.getText();
				String syntheticText=syntheticNumberField.getText();
				String analyticText=analyticNumberField.getText();
				kontoService.saveKontoClass(classText, classDescriptionField.getText());
				kontoService.saveKontoGroup(classText+groupText, groupDescriptionField.getText());
				kontoService.saveKontoSynthetic(classText+groupText+syntheticText, syntheticDescriptionField.getText());
				if (kontoSaveButton.getText().equals(messageService.getMessage("organization.sector.action.add"))){
					kontoService.saveKontoAnalythic(classText+groupText+syntheticText+analyticText, analyticDescriptionField.getText());
				}
				if (kontoSaveButton.getText().equals(messageService.getMessage("organiazation.sector.action.save"))){
					KontoSynthetic kontoSynthetic=kontoService.findKontoSynthetic(classText, groupText, syntheticText);
					selectedKonto.setKontoSynthetic(kontoSynthetic);
					selectedKonto.setValue(analyticText);
					selectedKonto.setDescription(analyticDescriptionField.getText());
					try {
						System.out.println("here");
						modelService.update(selectedKonto);
					}
					catch (Exception ex){
						JOptionPane.showMessageDialog(component, messageService.getMessage("conto.error.duplicate"),"Error", JOptionPane.ERROR_MESSAGE);
						System.out.println("Error updating konto");
					}
				}
//				if (selectedKonto!=null) {
////					modelService.delete(selectedKonto);
//					selectedKonto=null;
//				}
				clearData();
				refreshTableData();
				contoPlanDataPanel.setVisible(false);
			}
		});
        
        classNumberField.addKeyListener(new KeyListener() {
			
			@Override
			public void keyTyped(KeyEvent arg0) {
				String classText=classNumberField.getText();
				if (classText.length()!=0) {
					classNumberField.setText("");
					
				}

			}

			@Override
			public void keyPressed(KeyEvent e) {
				
			}

			@Override
			public void keyReleased(KeyEvent e) {
				if (!isNumber(classNumberField.getText()))
					classNumberField.setText("");
				refreshData();
			}
		});
        
        groupNumberField.addKeyListener(new KeyListener() {
			
			@Override
			public void keyTyped(KeyEvent arg0) {
				String groupText=groupNumberField.getText();
				if (groupText.length()!=0) {
					groupNumberField.setText("");
				}
			}
			
			@Override
			public void keyReleased(KeyEvent arg0) {
				if (!isNumber(groupNumberField.getText()))
					groupNumberField.setText("");
				refreshData();
			}
			@Override
			public void keyPressed(KeyEvent arg0) {
				
			}
		});
        
        syntheticNumberField.addKeyListener(new KeyListener() {
			
			@Override
			public void keyTyped(KeyEvent arg0) {
				String syntheticText=syntheticNumberField.getText();
				if (syntheticText.length()!=0) {
					syntheticNumberField.setText("");
				}
			}
			
			@Override
			public void keyReleased(KeyEvent arg0) {
				if (!isNumber(syntheticNumberField.getText()))
					syntheticNumberField.setText("");
				refreshData();
			}
			
			@Override
			public void keyPressed(KeyEvent arg0) {
			}
		});
        
        analyticNumberField.addKeyListener(new KeyListener() {
			
			@Override
			public void keyTyped(KeyEvent e) {
			}
			
			@Override
			public void keyReleased(KeyEvent e) {
				if (!isNumber(analyticNumberField.getText()))
					analyticNumberField.setText("");
				refreshData();
			}
			
			@Override
			public void keyPressed(KeyEvent e) {
			}
		});
    }
	
	private javax.swing.JPanel contoPlanTablePanel;
    private javax.swing.JTable contoTable;
    private javax.swing.JScrollPane contoTableScrollPane;
    private List<Konto> kontoList;
    private DefaultTableModel contoTableModel;
    private Konto selectedKonto;
    @SuppressWarnings("all")
	private void initContoPlanTablePanel(){
		contoPlanTablePanel = new javax.swing.JPanel();
		contoPlanTablePanel.setBorder(BorderFactory.createTitledBorder(messageService.getMessage("conto.inputed")));
		
        contoTableScrollPane = new javax.swing.JScrollPane();
        contoTable = new javax.swing.JTable();
        
        contoTableModel=new javax.swing.table.DefaultTableModel(
                new Object [][] {
                },
                new String [] {
                    messageService.getMessage("conto"),messageService.getMessage("conto.class"),
                    messageService.getMessage("conto.group"),messageService.getMessage("conto.synthetic"),
                    messageService.getMessage("conto.analytic"),messageService.getMessage("conto.analytic.description")
                }
            	) {
                Class[] types = new Class [] {
                    java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
                };
                boolean[] canEdit = new boolean [] {
                    false, false, false, false, false, false
                };

                public Class getColumnClass(int columnIndex) {
                    return types [columnIndex];
                }

                public boolean isCellEditable(int rowIndex, int columnIndex) {
                    return canEdit [columnIndex];
                }
        };
        
        contoTable.setModel(contoTableModel);
        contoTable.setAutoCreateRowSorter(true);   
        contoTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        contoTableScrollPane.setViewportView(contoTable);
        refreshTableData();
//        ListSelectionModel rowSelectionModel=contoTable.getSelectionModel();
//        rowSelectionModel.addListSelectionListener(new ListSelectionListener() {
//			
//			@Override
//			public void valueChanged(ListSelectionEvent arg0) {
//				int selectedRowIndex=contoTable.getSelectedRow();
//				if (selectedRowIndex>=0){
//					selectedRowIndex=contoTable.convertRowIndexToView(contoTable.getSelectedRow());
//					selectedKonto=kontoList.get(selectedRowIndex);
//					classNumberField.setText(selectedKonto.getKontoSynthetic().getKontoGroup().getKontoClass().getValue());
//					groupNumberField.setText(selectedKonto.getKontoSynthetic().getKontoGroup().getValue());
//					syntheticNumberField.setText(selectedKonto.getKontoSynthetic().getValue());
//					analyticNumberField.setText(selectedKonto.getValue());
//					refreshData();
//				}
//			}
//		});
	}
    
    private javax.swing.JPanel actionPanel;
    private javax.swing.JButton addContoButton;
    private javax.swing.JButton modifyContoButton;
    private void initActionPanel(){
    	actionPanel=new JPanel();
    	actionPanel.setBorder(BorderFactory.createTitledBorder(messageService.getMessage("organization.sector.action")));
    	addContoButton=new JButton();
    	addContoButton.setText(messageService.getMessage("conto.action.new"));
    	modifyContoButton=new JButton();
    	modifyContoButton.setText(messageService.getMessage("conto.action.modify"));
    	addContoButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				kontoSaveButton.setText(messageService.getMessage("organization.sector.action.add"));
				contoPlanDataPanel.setVisible(true);
				kontoSaveButton.setEnabled(false);
				clearData();
			}
		});
    	
    	modifyContoButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				if (contoTable.getSelectedRow()>=0){
					int rowIndex=contoTable.convertRowIndexToModel(contoTable.getSelectedRow());
					selectedKonto=kontoList.get(rowIndex);
					kontoSaveButton.setText(messageService.getMessage("organiazation.sector.action.save"));
					classNumberField.setText(selectedKonto.getKontoSynthetic().getKontoGroup().getKontoClass().getValue());
					groupNumberField.setText(selectedKonto.getKontoSynthetic().getKontoGroup().getValue());
					syntheticNumberField.setText(selectedKonto.getKontoSynthetic().getValue());
					analyticNumberField.setText(selectedKonto.getValue());
					kontoSaveButton.setEnabled(true);
					refreshData();
					contoPlanDataPanel.setVisible(true);
				}
			}
		});
    }
	private void setLayout(){
		javax.swing.GroupLayout contoPlanDataPanelLayout = new javax.swing.GroupLayout(contoPlanDataPanel);
        contoPlanDataPanel.setLayout(contoPlanDataPanelLayout);
        contoPlanDataPanelLayout.setHorizontalGroup(
            contoPlanDataPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(contoPlanDataPanelLayout.createSequentialGroup()
                .addGap(16, 16, 16)
                .addGroup(contoPlanDataPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(contoLabel)
                    .addComponent(classNumberLabel)
                    .addComponent(groupNumberLabel)
                    .addComponent(syntheticNumberLabel)
                    .addComponent(analyticNumberLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(contoPlanDataPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(contoPlanDataPanelLayout.createSequentialGroup()
                        .addComponent(contoField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(kontoSaveButton))
                    .addGroup(contoPlanDataPanelLayout.createSequentialGroup()
                        .addGroup(contoPlanDataPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(analyticNumberField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(syntheticNumberField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(classNumberField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(groupNumberField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(contoPlanDataPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(classDescriptionField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(groupDescriptionField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(contoPlanDataPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(analyticDescriptionField)
                                .addComponent(syntheticDescriptionField)))))
                .addContainerGap(19, Short.MAX_VALUE))
        );
        contoPlanDataPanelLayout.setVerticalGroup(
            contoPlanDataPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(contoPlanDataPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(contoPlanDataPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(classNumberLabel)
                    .addComponent(classNumberField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(classDescriptionField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(contoPlanDataPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(groupNumberLabel)
                    .addComponent(groupDescriptionField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(groupNumberField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(contoPlanDataPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(syntheticNumberField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(syntheticNumberLabel)
                    .addComponent(syntheticDescriptionField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(contoPlanDataPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(analyticNumberField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(analyticNumberLabel)
                    .addComponent(analyticDescriptionField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(contoPlanDataPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(contoLabel)
                    .addComponent(contoField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(kontoSaveButton))
                .addContainerGap(19, Short.MAX_VALUE))
        );
        javax.swing.GroupLayout contoPlanTablePanelLayout = new javax.swing.GroupLayout(contoPlanTablePanel);
        contoPlanTablePanel.setLayout(contoPlanTablePanelLayout);
        contoPlanTablePanelLayout.setHorizontalGroup(
            contoPlanTablePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(contoPlanTablePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(contoTableScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 858, Short.MAX_VALUE)
                .addContainerGap())
        );
        contoPlanTablePanelLayout.setVerticalGroup(
            contoPlanTablePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(contoPlanTablePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(contoTableScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(59, Short.MAX_VALUE))
        );
        javax.swing.GroupLayout actionPanelLayout = new javax.swing.GroupLayout(actionPanel);
        actionPanel.setLayout(actionPanelLayout);
        actionPanelLayout.setHorizontalGroup(
            actionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(actionPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(addContoButton)
                .addGap(18, 18, 18)
                .addComponent(modifyContoButton)
                .addContainerGap(104, Short.MAX_VALUE))
        );
        actionPanelLayout.setVerticalGroup(
            actionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(actionPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(actionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addContoButton)
                    .addComponent(modifyContoButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(actionPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(contoPlanDataPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(contoPlanTablePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(contoPlanDataPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(actionPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(contoPlanTablePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(221, Short.MAX_VALUE))
        );
		
	}
	
	/**
		Refreshes all of the descriptions of the Konto parts
	 */
	private void refreshData(){
		try {
			KontoClass kontoClass=kontoService.findKontoClass(classNumberField.getText());
			classDescriptionField.setText(kontoClass.getDescription());
		}
		catch (Exception ex){
			classDescriptionField.setText("");
		}
		try {
			KontoGroup kontoGroup=kontoService.findKontoGroup(classNumberField.getText(),groupNumberField.getText());
			groupDescriptionField.setText(kontoGroup.getDescription());
		}
		catch (Exception ex){
			groupDescriptionField.setText("");
		}
		try {
			KontoSynthetic kontoSynthetic=kontoService.findKontoSynthetic(classNumberField.getText(),groupNumberField.getText(),syntheticNumberField.getText());
			syntheticDescriptionField.setText(kontoSynthetic.getDescription());
		}
		catch (Exception ex){
			syntheticDescriptionField.setText("");
		}
		try {
			Konto kontoAnalytic=kontoService.findKonto(classNumberField.getText(),groupNumberField.getText(),syntheticNumberField.getText(),analyticNumberField.getText());
			analyticDescriptionField.setText(kontoAnalytic.getDescription());
		}
		catch (Exception ex){
			analyticDescriptionField.setText("");
		}
		contoField.setText(classNumberField.getText()+groupNumberField.getText()+syntheticNumberField.getText()+analyticNumberField.getText());
		if (groupNumberField.getText().equals("")||classNumberField.getText().equals("")||syntheticNumberField.getText().equals("")||analyticNumberField.getText().equals("")){
			kontoSaveButton.setEnabled(false);
		}
		else {
			kontoSaveButton.setEnabled(true);
		}
			
	}
	
	private boolean isNumber(String string){
		try {
			Integer.parseInt(string);
			return true;
		}
		catch (Exception e){
			return false;
		}
	}
	
	@SuppressWarnings("all")
	private void refreshTableData() {
		kontoList = modelService.findAll(Konto.class);
		contoTableModel.getDataVector().removeAllElements();
		contoTable.setAutoCreateRowSorter(true);
		for (Konto konto:kontoList){
			Vector row = new Vector();
			row.add(konto.getKontoSynthetic().getKontoGroup().getKontoClass().getValue()+konto.getKontoSynthetic().getKontoGroup().getValue()+konto.getKontoSynthetic().getValue()+konto.getValue());
			row.add(konto.getKontoSynthetic().getKontoGroup().getKontoClass().getValue());
			row.add(konto.getKontoSynthetic().getKontoGroup().getValue());
			row.add(konto.getKontoSynthetic().getValue());
			row.add(konto.getValue());
			row.add(konto.getDescription());
			contoTableModel.addRow(row);
		}
	}
	
	private void clearData(){
		classNumberField.setText("");
		groupNumberField.setText("");
		analyticNumberField.setText("");
		syntheticNumberField.setText("");
		refreshData();
	}
}
